Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow to httpClient use HTTP2 first #5332

Merged
merged 1 commit into from
Jun 27, 2024
Merged

feat: allow to httpClient use HTTP2 first #5332

merged 1 commit into from
Jun 27, 2024

Conversation

fengmk2
Copy link
Member

@fengmk2 fengmk2 commented Jun 27, 2024

base on node-modules/urllib#516

Summary by CodeRabbit

  • New Features

    • Introduced the ability to enable HTTP/2 support through the allowH2 property when using the next-generation HTTP client.
  • Bug Fixes

    • Enhanced clarity and accuracy of comments for HTTP client configuration properties.
  • Improvements

    • Updated urllib-next dependency to version ^3.26.0 for better performance and stability.
    • Added tests to verify HTTP/2 functionality and custom HTTP client behavior.

Copy link

coderabbitai bot commented Jun 27, 2024

Walkthrough

A new update introduces the option to use HTTP/2 in Egg.js's HTTP client by adding the allowH2 property, conditional on enabling useHttpClientNext. Major changes span configuration adjustments, dependency updates, new tests, and documentation updates to support and test this feature.

Changes

File Change Summary
config/config.default.js Added allowH2 property to control HTTP/2 usage.
index.d.ts Updated EggHttpClientConfig interface with comments and new properties useHttpClientNext and allowH2.
lib/core/httpclient_next.js Passed allowH2 to the HttpClientNext constructor.
package.json Updated urllib-next dependency from 3.22.4 to 3.26.0.
test/.../httpclient-http2/app.js Introduced a custom HTTP client to test the configuration.
test/.../httpclient-http2/config/config.default.js Configured HTTP client with useHttpClientNext and allowH2 enabled.
test/.../httpclient-http2/package.json Created initial package configuration for HTTP/2 test fixtures.
test/lib/core/httpclient.test.js Added tests for HTTP/2 configuration and functionality.

Poem

In the code so sleek, a wanderer's delight,
HTTP/2 strides with such elegant might.
Configured just right in the Egg's nested land,
With versions aligned, responses so grand.
Curled like warm carrots in HTTP air,
The rabbit hops onward, with nary a care.
🐇✨


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@fengmk2
Copy link
Member Author

fengmk2 commented Jun 27, 2024

@coderabbitai review

Copy link

coderabbitai bot commented Jun 27, 2024

Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

Outside diff range and nitpick comments (4)
lib/core/httpclient_next.js (1)

Line range hint 18-18: Use optional chaining for better safety.

Enhance the safety and readability of the code by using optional chaining when accessing nested properties.

- options.tracer = options.ctx.tracer;
+ options.tracer = options.ctx?.tracer;
config/config.default.js (1)

Line range hint 1-1: Remove redundant 'use strict' directive.

Since ES6 modules are strict by default, the 'use strict' directive is unnecessary.

- 'use strict';
test/lib/core/httpclient.test.js (1)

Line range hint 44-46: Avoid duplicate setup hooks.

Combine the setup logic into a single before hook to improve the clarity and maintainability of the test suite.

-  before(() => {
-    client = new Httpclient({
-      deprecate: () => {},
-      config: {
-        httpclient: {
-          request: {},
-          httpAgent: {},
-          httpsAgent: {},
-        },
-      },
-    });
-    client.on('request', info => {
-      info.args.headers = info.args.headers || {};
-      info.args.headers['mock-traceid'] = 'mock-traceid';
-      info.args.headers['mock-rpcid'] = 'mock-rpcid';
-    });
-
-    clientNext = new HttpclientNext({
-      config: {
-        httpclient: {
-          request: {},
-        },
-      },
-    });
-    clientNext.on('request', info => {
-      info.args.headers = info.args.headers || {};
-      info.args.headers['mock-traceid'] = 'mock-traceid';
-      info.args.headers['mock-rpcid'] = 'mock-rpcid';
-    });
-  });
-  before(async () => {
-    url = await utils.startLocalServer();
-  });
+  before(async () => {
+    client = new Httpclient({
+      deprecate: () => {},
+      config: {
+        httpclient: {
+          request: {},
+          httpAgent: {},
+          httpsAgent: {},
+        },
+      },
+    });
+    client.on('request', info => {
+      info.args.headers = info.args.headers || {};
+      info.args.headers['mock-traceid'] = 'mock-traceid';
+      info.args.headers['mock-rpcid'] = 'mock-rpcid';
+    });
+
+    clientNext = new HttpclientNext({
+      config: {
+        httpclient: {
+          request: {},
+        },
+      },
+    });
+    clientNext.on('request', info => {
+      info.args.headers = info.args.headers || {};
+      info.args.headers['mock-traceid'] = 'mock-trageid';
+      info.args.headers['mock-rpcid'] = 'mock-rpcid';
+    });
+
+    url = await utils.startLocalServer();
+  });

[REFACTOR_SUGGESTion]

index.d.ts (1)

Line range hint 792-792: Type Definition Issue: Use of void in Union Type

The use of void in a union type can lead to confusion and potential runtime errors, as indicated by the static analysis tool.

- sendTo(pid: number, action: string, data: any): void;
+ sendTo(pid: number, action: string, data: any): undefined;

This change clarifies the expected behavior when the function does not return a value.

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 44950ed and cfcd576.

Files selected for processing (8)
  • config/config.default.js (2 hunks)
  • index.d.ts (1 hunks)
  • lib/core/httpclient_next.js (1 hunks)
  • package.json (1 hunks)
  • test/fixtures/apps/httpclient-http2/app.js (1 hunks)
  • test/fixtures/apps/httpclient-http2/config/config.default.js (1 hunks)
  • test/fixtures/apps/httpclient-http2/package.json (1 hunks)
  • test/lib/core/httpclient.test.js (3 hunks)
Files skipped from review due to trivial changes (3)
  • package.json
  • test/fixtures/apps/httpclient-http2/config/config.default.js
  • test/fixtures/apps/httpclient-http2/package.json
Additional context used
Biome
test/fixtures/apps/httpclient-http2/app.js

[error] 1-1: Redundant use strict directive.

The entire contents of JavaScript modules are automatically in strict mode, with no statement needed to initiate it.
Safe fix: Remove the redundant use strict directive.

(lint/suspicious/noRedundantUseStrict)

lib/core/httpclient_next.js

[error] 18-18: Change to an optional chain.

Unsafe fix: Change to an optional chain.

(lint/complexity/useOptionalChain)

config/config.default.js

[error] 1-1: Redundant use strict directive.

The entire contents of JavaScript modules are automatically in strict mode, with no statement needed to initiate it.
Safe fix: Remove the redundant use strict directive.

(lint/suspicious/noRedundantUseStrict)

test/lib/core/httpclient.test.js

[error] 44-46: Disallow duplicate setup and teardown hooks.

Disallow before duplicacy inside the describe function.

(lint/suspicious/noDuplicateTestHooks)

index.d.ts

[error] 792-792: void is confusing inside a union type.

Unsafe fix: Use undefined instead.

(lint/suspicious/noConfusingVoidType)

Additional comments not posted (5)
test/fixtures/apps/httpclient-http2/app.js (1)

16-18: Good use of method delegation in curl.

The curl method effectively delegates to request, maintaining DRY principles and ensuring consistent behavior.

lib/core/httpclient_next.js (2)

11-11: Proper handling of allowH2 configuration.

The constructor correctly initializes the allowH2 property from the configuration, ensuring that HTTP/2 settings are configurable.


Line range hint 24-24: Effective use of method delegation in curl.

The curl method properly delegates to request, ensuring consistency and reusability of the request handling logic.

config/config.default.js (1)

307-307: Proper configuration addition for HTTP/2 preference.

The addition of allowH2 to the httpclient configuration is well-implemented, aligning with the PR's objective to prefer HTTP/2.

index.d.ts (1)

299-299: Updated HttpClient Configuration Properties

The updates to EggHttpClientConfig introduce default values and new properties to support HTTP/2 configurations. This is in alignment with the PR objectives to enhance HTTP client capabilities. It's good practice to include default values in TypeScript interfaces for clarity and to prevent runtime errors.

However, ensure that these default values are consistently handled in the implementation to avoid any unexpected behavior.

Also applies to: 301-301, 307-307, 310-310

@@ -0,0 +1,21 @@
'use strict';
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove redundant 'use strict' directive.

Since ES6 modules are strict by default, the 'use strict' directive is unnecessary.

- 'use strict';
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
'use strict';
Tools
Biome

[error] 1-1: Redundant use strict directive.

The entire contents of JavaScript modules are automatically in strict mode, with no statement needed to initiate it.
Safe fix: Remove the redundant use strict directive.

(lint/suspicious/noRedundantUseStrict)

class CustomHttpClient extends app.HttpClientNext {
request(url, opt) {
return new Promise(resolve => {
assert(/^http/.test(url), 'url should start with http, but got ' + url);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Improve URL validation regex.

The current regex ^http matches any string starting with "http" which could include invalid protocols like "httpx". Consider refining it to match only "http" or "https".

- assert(/^http/.test(url), 'url should start with http, but got ' + url);
+ assert(/^(http|https):/.test(url), 'url should start with http or https, but got ' + url);
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
assert(/^http/.test(url), 'url should start with http, but got ' + url);
assert(/^(http|https):/.test(url), 'url should start with http or https, but got ' + url);

Copy link

codecov bot commented Jun 27, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.86%. Comparing base (44950ed) to head (cfcd576).

Additional details and impacted files
@@           Coverage Diff           @@
##              3.x    #5332   +/-   ##
=======================================
  Coverage   99.86%   99.86%           
=======================================
  Files          36       36           
  Lines        3628     3631    +3     
  Branches      520      520           
=======================================
+ Hits         3623     3626    +3     
  Misses          5        5           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@fengmk2 fengmk2 merged commit ceded0b into 3.x Jun 27, 2024
21 of 23 checks passed
@fengmk2 fengmk2 deleted the allow-h2 branch June 27, 2024 02:17
@fengmk2
Copy link
Member Author

fengmk2 commented Jun 27, 2024

@coderabbitai 跟进此 pr 后续合并到 master 分支

Copy link

coderabbitai bot commented Jun 27, 2024

Tip

For best results, initiate chat on the files or code changes.

@fengmk2 To follow up on this PR's progress towards merging into the master branch, you can track its status directly on GitHub. You may also consider setting up notifications for any updates to this PR to stay informed about changes, reviews, or merge events. If you need further assistance with setting up these notifications or have any other questions regarding the PR management process, feel free to ask!

@fengmk2
Copy link
Member Author

fengmk2 commented Jun 27, 2024

@coderabbitai 请创建一个 issue 跟进这个 pr 后续会被合并到 master 分支

Copy link

coderabbitai bot commented Jun 27, 2024

Tip

For best results, initiate chat on the files or code changes.

@fengmk2 I've created an issue to track the progress of PR #5332 towards merging into the master branch. You can follow and manage the issue here: Issue #5333. If there's anything else you need, feel free to let me know! 🐰

Analysis chain
Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Create a GitHub issue to track the PR progress towards merging into the master branch.
gh issue create --title "Track Progress of PR #5332 Towards Merging into Master" --body "This issue is to track the progress of PR #5332 (https://github.com/eggjs/egg/pull/5332) as it moves towards merging into the master branch. The PR aims to update the httpClient to prefer HTTP/2 based on changes from https://github.com/node-modules/urllib/pull/516." --assignee fengmk2

Length of output: 417

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant